package com.tianpengtech.common.mock.impl.string;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import com.tianpengtech.common.mock.impl.AbstractStringMock;
import com.tianpengtech.common.util.DateUtils;

public class DataMock extends AbstractStringMock {

	private String start;
	private String end;
	private String formatStr;
	public DataMock(String start,String end,String format){
		this.start = start;
		this.end = end;
		this.formatStr = format;
	}
	@Override
	public String mockData() {
		Date randomDate = randomDate(start,end);
		return DateUtils.formateData(randomDate,formatStr);
	}

	private Date randomDate(String beginDate, String endDate) {
		try {
			SimpleDateFormat format = new SimpleDateFormat(formatStr);
			Date start = format.parse(beginDate);// 开始日期
			Date end = format.parse(endDate);// 结束日期
			if (start.getTime() >= end.getTime()) {
				return null;
			}
			long date = random(start.getTime(), end.getTime());

			return new Date(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private long random(long begin, long end) {
		long rtnn = begin + (long) (Math.random() * (end - begin));
		if (rtnn == begin || rtnn == end) {
			return random(begin, end);
		}
		return rtnn;
	}

}
